<%-- 
    Document   : manageEvents
    Created on : 27-mar-2014, 14.44.23
    Author     : Mark
--%>

<%@page import="it.evento.EventPrintable"%>
<%@page import="it.evento.Events"%>
<%@page import="java.util.List"%>
<%@page import="it.evento.controllers.Controller"%>
<%@page import="it.evento.utils.genpage.Article"%>
<%@page import="it.evento.utils.genpage.ArticleRow"%>
<%@page import="it.evento.utils.genpage.RowCell"%>
<%@page import="it.evento.Users"%>
<%@page import="it.evento.utils.genpage.BlogPost"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
    boolean isLoggedUser = false;
    Users loggedUser = null;
    
    if(session.getAttribute("loggedUser")!=null) {
        loggedUser = (Users)session.getAttribute("loggedUser");
        isLoggedUser = true;
    }
    else {
        isLoggedUser = false;
    }
%>    

<!DOCTYPE html>
<html dir="ltr" lang="it-IT">
    <head>
        <title>EvenTO</title>

        <jsp:include page="html/metadata.jsp"/>
        
         <script>
            $(function() {                
                $('#cancelEvents').click(function(e) {
                   console.log("[Cancel Events]");
                   var delArray = [];                 
                   $('input[type=checkbox]').each(function () {
                       console.log("chckbox id: "+this.id+" checked? "+this.checked);
                       if(this.checked)
                           delArray.push(this.id);
                   });
                   console.log("delArray: "+delArray.toString());
                   
                   if(delArray.length>0){
                    $.post("Controller",
                                 {type: "cancelEvents",
                                     idEvents: delArray.toString()
                                 }, 
                             function(data) {                            
                                if (data.status === 0) {
                                    $('#msgOut').html("<ul>" + data.message+"</ul>");
                                    location.reload();
                                }
                                else {
                                    $('#msgOut').html("<ul>" + data.status + ": " + data.message+"</ul>");
                                }
                            },
                                 'json');
                   }else{
                       $('#msgOut').html("<ul><li>Selezionare un evento da rimuovere</li></ul>");
                   }
                   console.log("[/Cancel Events]");
                });                
               
                var modArray=[];
                $('#modEvents').click(function(e) {
                   console.log("[Mod Events]");        
                   modArray= []; //ripulisce array
                   $('#msgOut').html(" ");
                   $('input[type=checkbox]').each(function () {
                       console.log("chckbox id: "+this.id+" checked? "+this.checked);
                       if(this.checked)                          
                           modArray.push(this.id);
                               
                   });
                   //console.log("modArray "+modArray.toString());
                   if(modArray.length>0){
                        $.post("Controller",
                                     {type: "forwardEvents",
                                         idEvents: modArray.toString()
                                     },
                             function(data) {
                                 if (data.status === 0) {                                     
                                    $('#htmlModify').html(data.message);                                        
                                 }else {
                                    $('#msgOut').html("<ul>" + data.status + ": " + data.message+"</ul>");
                                 }
                             },
                                     'json');
                    }else{
                       $('#msgOut').html("<ul><li>Selezionare un evento da modificare</li></ul>");
                   }
                   console.log("[/Mod Events]");
                });
                               
                $("#htmlModify").on('click', '.evento-button', function() {
                    var recupero_id = $(this).attr("id");
                    var split=recupero_id.split('_');
                    console.log("id: "+split[0]);
                    console.log("type: "+split[1]);
                    modArray.splice(modArray.indexOf(split[0]), 1);//cancella elemento dalla lista
                    if(split[1]==="add"){                        
                        $.post("Controller",
                                        {type: "modifyEvents",
                                            idModEvent:split[0],
                                            nameModEvent: $('#'+split[0]+'_name').val(),
                                            descModEvent: $('#'+split[0]+'_desc').val(),
                                            locModEvent: $('#'+split[0]+'_loc').val(),
                                            typeModEvent: $('#'+split[0]+'_type').val(),
                                            startModEvent: $('#'+split[0]+'_start').val(),
                                            endModEvent: $('#'+split[0]+'_end').val(),
                                            idEvents: modArray.toString()
                                        },
                                function(data) {
                                    if (data.status === 0) {                                         
                                       $('#htmlModify').html(data.message);                                        
                                    }else {
                                       $('#msgOut').html("<ul>"+ data.message+"</ul>");
                                       $('#htmlModify').html(""); 
                                    }
                                },
                                        'json');
                   }else{//===cancel ricarica la pagina senza quel form di modifica
                       $.post("Controller",
                                     {type: "forwardEvents",
                                         idEvents: modArray.toString()
                                     },
                             function(data) {
                                 if (data.status === 0) {                                     
                                    $('#htmlModify').html(data.message);                                        
                                 }else {
                                    $('#htmlModify').html("");   
                                 }
                             },
                                     'json');
                   }
                  
                 });
 
    });
                
            
         </script>
    </head>
    <body>
        <div id="evento-main">
            <!-- header -->
            <jsp:include page="html/header.jsp" />
            <!-- /header -->

            <!-- top-menu -->
            <%@include file="html/top-menu.jsp" %>
            <!-- /top-menu -->

            <div class="evento-sheet clearfix">
                <div class="evento-layout-wrapper">
                    <div class="evento-content-layout">
                        <div class="evento-content-layout-row">
                            <div class="evento-layout-cell evento-content">
                                <% if(isLoggedUser) { %>
                                    <%
                                         List<EventPrintable> eventsList=(List<EventPrintable>) request.getAttribute("eventsList");
                                        
                                         String s="<table border='1'>";
                                         s+="<tr>"
                                                 + "<td> </td>"
                                                 + "<td>Nome</td>"
                                                 + "<td>Descrizione</td>"
                                                 + "<td>Location</td>"                                                 
                                                 + "<td>Tipo</td>"
                                                 + "<td>StartDate</td>"
                                                 + "<td>EndDate</td>"                                                 
                                                 + "</tr>";
                                         
                                         for(EventPrintable e:eventsList){
                                             s+="<tr>";
                                             s+="<td><input type='checkbox'  id='"+e.getId()+"'/></td>";
                                             s+="<td>"+e.getName()+"</td>";
                                             s+="<td>"+e.getDescription()+"</td>";
                                             s+="<td>"+e.getLocation()+"</td>";
                                             s+="<td>"+e.getType()+"</td>";
                                             s+="<td>"+e.getStartDate()+"</td>";
                                             s+="<td>"+e.getEndDate()+"</td>";                                            
                                             s+="</tr>";                                                 
                                         }
                                         s+="</table>";                                         
                                         s+="<input type='button' value='Modifica' id='modEvents' class='evento-button' />";
                                         s+="<input type='button' value='Elimina' id='cancelEvents' class='evento-button' />";
                                         s+="<a href='createEvent.jsp'><input type='button' value='Aggiungi' id='createEvents' class='evento-button' /></a>";
                                         s+="<div id='msgOut'></div>";
                                    %>     
                                    <%=
                                           new BlogPost()
                                                   .setTitle("Your Events")
                                                   .setContent(s)
                                                   .print()
                                     %>  
                                     <div id="htmlModify"></div>
                                <% }else{ %>                     
                                    <h3>Non hai i diritti necessari per accedere a questa pagina</h3>
                                <%}%>
                                        
                                  
                                     
                                
                                
                               
                            </div>

                            <!-- sidebar -->
                            <jsp:include page="html/sidebar.jsp" />
                            <!-- /sidebar -->
                            
                        </div>
                    </div>
                </div>

                <!-- footer -->
                <%@include file="html/footer.jsp" %>
                <!-- /footer -->

            </div>
        </div>
    </body>
</html>
